<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.mobilePhone" placeholder="手机号码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.idNumber" placeholder="身份证号码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.city" placeholder="城市" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.channel" placeholder="获客渠道" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-if="isAuth('customer:customer:save')"
          type="primary"
          @click="addOrUpdateHandle()"
        >新增</el-button>
        <el-button
          v-if="isAuth('customer:customer:messaging')"
          type="primary"
          @click="sendMessages()"
          :disabled="dataListSelections.length <= 0"
        >发送短信</el-button>
        <el-button
          v-if="isAuth('customer:customer:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-upload
          v-if="isAuth('customer:customer:upload')"
          class="btn"
          :action="uploadUrl"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :showFileList="false"
        >
          <el-button type="primary">
            批量导入
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <a v-if="isAuth('customer:customer:upload')" :href="templateDownloadUrl">导入模板下载</a>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" header-align="center" align="center" label="ID"></el-table-column>
      <el-table-column
        prop="mobilePhone"
        header-align="center"
        align="center"
        label="手机号码"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="sourceFile"
        header-align="center"
        align="center"
        label="来源文件"
        width="100"
      ></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="姓名" width="180"></el-table-column>
      <el-table-column prop="area" header-align="center" align="center" label="区域" width="100"></el-table-column>
      <el-table-column prop="city" header-align="center" align="center" label="城市" width="100"></el-table-column>
      <el-table-column prop="channel" header-align="center" align="center" label="渠道" width="100"></el-table-column>
      <el-table-column
        prop="channelSource"
        header-align="center"
        align="center"
        label="名单来源"
        width="100"
      ></el-table-column>
      <el-table-column prop="label" header-align="center" align="center" label="获客标签" width="180"></el-table-column>
      <el-table-column prop="idNumber" header-align="center" align="center" label="身份证" width="280"></el-table-column>
      <el-table-column
        prop="gender"
        header-align="center"
        align="center"
        :formatter="this.genderTableCellFormat"
        label="性别"
      ></el-table-column>
      <el-table-column
        prop="birthDay"
        header-align="center"
        align="center"
        :formatter="this.dateTableCellFormat"
        label="生日"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="operatorName"
        header-align="center"
        align="center"
        :formatter="this.operatorTableCellFormat"
        label="运营商"
      ></el-table-column>
      <el-table-column
        prop="brandName"
        header-align="center"
        align="center"
        label="运营商品牌"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="ownHouse"
        header-align="center"
        align="center"
        :formatter="this.boolValueTableCellFormat"
        label="是否购房"
      ></el-table-column>
      <el-table-column
        prop="houseBuyTime"
        header-align="center"
        align="center"
        :formatter="this.dateTableCellFormat"
        label="房产购买时间"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="monthlyIncome"
        header-align="center"
        align="center"
        label="税后收入"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="ownCar"
        header-align="center"
        align="center"
        :formatter="this.boolValueTableCellFormat"
        label="是否购车"
      ></el-table-column>
      <el-table-column
        prop="carPledge"
        header-align="center"
        align="center"
        :formatter="this.boolValueTableCellFormat"
        label="车辆抵押"
      ></el-table-column>
      <el-table-column
        prop="depositPeriod"
        header-align="center"
        align="center"
        label="公积金月数"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="pinganInsuranceTime"
        header-align="center"
        align="center"
        :formatter="this.dateTableCellFormat"
        label="平安寿险购买时间"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        :formatter="this.datetimeTableCellFormat"
        label="创建时间"
        width="180"
      ></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './customer-add-or-update'
export default {
  data () {
    return {
      dataForm: {
        mobilePhone: '',
        idNumber: '',
        name: '',
        city: '',
        channel: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      uploadUrl: this.$http.adornUrl(`/customer/customer/upload?token=${this.$cookie.get('token')}`),
      templateDownloadUrl: this.$http.adornUrl(`/customer/customer/download-template?token=${this.$cookie.get('token')}`)
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/customer/customer/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          ...this.dataForm
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 发送短信
    sendMessages (id) {
      let ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '发送短信' : '批量发送短信'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/customer/customer/messaging'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/customer/customer/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    // 批量上传成功
    handleUploadSuccess (res) {
      this.$message({
        message: res.msg || '操作成功',
        type: 'success',
        duration: 3000,
        onClose: () => {
          this.getDataList()
        }
      })
    },
    // 批量上传失败
    handleUploadError (err) {
      console.log(err)
      this.$message.error('批量导入失败')
    }
  }
}
</script>
